package cc.rengu.igas.channel.cups.core.service.base;

import cc.rengu.igas.channel.cups.common.constant.CupsParamConstant;
import cc.rengu.igas.channel.cups.common.constant.CupsTreeNodeConstant;
import cc.rengu.igas.channel.cups.common.enums.RespCodeEnum;
import cc.rengu.jradp.mods.trmsg.iso8583.MacUtil;
import cc.rengu.oltp.service.base.BizBaseService;
import cc.rengu.oltp.service.model.RiskMonitorInfo;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;

/**
 * cups管理类交易基类
 */
public abstract class ManageService extends BizBaseService {
    @Override
    protected Object beforeBizProcess() throws Exception {
        /*计算macblock*/
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        MacUtil keyMac = (MacUtil) UnifiedCache.get(CupsParamConstant.CUPS_8583_MAC_CACHE_NAME,
                CupsParamConstant.CUPS_8583_MAC_FILE +
                        CupsParamConstant.CUPS_8583_MAC_TYPE_KEY);
        if (keyMac == null) {
            rglog.error("获取mac域配置失败");
            throw new BizException(RespCodeEnum.GENERATE_MAC_BLOCK_ERROR.getRespCode(), RespCodeEnum.GENERATE_MAC_BLOCK_ERROR.getRespDesc());
        }
        byte[] macBlock = keyMac.getMacBlock(CupsParamConstant.CUPS_8583_MAC_TYPE_KEY);
        if (macBlock == null) {
            throw new BizException(RespCodeEnum.GENERATE_MAC_BLOCK_ERROR.getRespCode(), RespCodeEnum.GENERATE_MAC_BLOCK_ERROR.getRespDesc());
        }
        xmlTreeUtil.setXmlTreeStringValue(CupsTreeNodeConstant.CUPS_MAC_BLOCK, new String(macBlock));

        return bizBeforeCust();
    }

    @Override
    protected BizResponse callBizService(Object request) throws Exception {
        return bizProcessCust(request);
    }

    @Override
    protected void afterBizProcess(BizResponse response) throws Exception {
        bizAfterCust(response);
    }

    @Override
    protected RiskMonitorInfo getRiskMonitorInfo() {
        return null;
    }

    /**
     * 业务处理前客户化处理
     *
     * @return 业务处理实体类
     */
    protected abstract Object bizBeforeCust() throws Exception;

    /**
     * 业务逻辑处理客户化处理
     *
     * @param request 业务处理前的返回实体类
     * @return 业务处理结果
     */
    protected abstract BizResponse bizProcessCust(Object request) throws Exception;

    /**
     * 业务处理后逻辑处理
     *
     * @param response 业务调用响应信息
     */
    protected abstract void bizAfterCust(Object response) throws Exception;
}
